class WeeklyResultsController < ApplicationController
  caches_page :one, :top
  def one
    result = WeeklyResult.find_by_score_rank_and_weekly_id params[:rank], params[:id]
    work =Work.find_by_wid result.wid, :select => "wid, cdate, pic_path"
    last_score_rank = result.last_score_rank
    is_changqi = result.is_changqi?
    respond_to do |format|
      format.yaml { render :text => [result, work, last_score_rank, is_changqi].to_yaml, :content_type => 'text/yaml'}
      #format.xml { render :xml => result.to_xml }
    end
  end
  
  def top
    weekly = Weekly.find params[:id]
    last_weekly = weekly.last
    last_rank = WeeklyResult.find_by_weekly_id_and_score_rank weekly.id, params[:num]
    nilscore_count = WeeklyResult.count :conditions => ["weekly_id = ? and score_rank is null and score >= ?", weekly.id, last_rank.score]
    results = WeeklyResult.all_info :conditions => ["wr.weekly_id = ?", params[:id]], :limit => params[:num].to_i + nilscore_count
    results.each{|r| r[:space] = Work.find_by_wid(r.attributes['wid']).show_space_info(true) if r.sp_type_id == 1; r.score = r.score.to_i}
    wids = results.collect{|r| r.attributes['wid']}
    works = Work.find_all_by_wid wids, :select => "wid, cdate, pic_path"
    wids = results.select{|r| r.score_rank}.collect{|r| r.attributes['wid']}
    #上期排名
    last_ranks = {}
    if last_weekly
      wids.select{|w| w > last_weekly.wid}.each{|w| last_ranks[w] = 0; wids.delete w }
      last_ranks.merge! WeeklyResult.sum('score_rank', :include => :weekly_result_work,
            :conditions =>["weekly_id = ? and wid in (?)", weekly.last.id, wids],
            :group => "wid")
      WeeklyExcept.all(:conditions =>["weekly_id = ? and wid in (?)", last_weekly.id, wids]).each{|we| last_ranks[we.wid] = -1}
      WeeklyResult.count( :include => [:weekly, :weekly_result_work],
              :conditions => ['weeklies."index" < ? and weekly_result_works.wid in (?)', last_weekly.index, wids.reject{|w| last_ranks[w] }],
              :group => "weekly_result_works.wid").each{|k, v| last_ranks[k] = -222}
      wids.each do |w|
        last_ranks[w] ||= 0
        last_ranks[w] = nil if last_ranks[w] == -222
      end
      is_changqi_hash = WeeklyResult.count('weekly_results.wid', :include => [:weekly, :weekly_result_work],
          :conditions => ["score_rank <= weeklies.rank_from and weekly_result_works.wid in (?) and weeklies.id < ?", wids.reject{|w| last_ranks[w] &&  last_ranks[w].zero? }, params[:id]],
          :group => 'weekly_result_works.wid')
    else
      wids.each{|w| last_ranks[w] = 0 }
      is_changqi_hash = {}
    end
    #新番上期分数
    wids = results.select{|r| r.score_rank.nil?}.collect{|r| r.attributes['wid']}
    wids.select{|w| w > last_weekly.wid}.each{|w| last_ranks[w] = 0; wids.delete w }
    last_ranks.merge! WeeklyResult.sum('score', :include => :weekly_result_work,
            :conditions =>["weekly_id = ? and wid in (?)", weekly.last.id, wids],
            :group => "wid")
    respond_to do |format|
      format.yaml { render :text => [results, works, last_ranks, is_changqi_hash].to_yaml, :content_type => 'text/yaml'}
      #format.xml { render :xml => results.to_xml }
    end
  end
  
  #
  def info
    ha = {}
    ha[:no] = params[:id]
    weekly = Weekly.find(params[:id])
    ha[:name] = weekly.name
    ha[:wid] = weekly.wid
    ha[:rank_from] = weekly.rank_from
    ha[:count] = WeeklyResult.count :conditions => ["weekly_id = ?", weekly.index]
    ha[:pic] = Work.find_by_wid(weekly.wid).pic_path rescue nil
    respond_to do |format|
      format.yaml { render :text => ha.to_yaml, :content_type => 'text/yaml'}
    end
  end

  private
  def top_data

  end

end
